import os
import sys
import md5
import library
import traceback

class m_login(object):
    def __init__(self, parent=None):
        super(m_login, self).__init__()
        self.db = library.db()
        self.conn = self.db.makeConn()
        self.curs = self.conn.cursor()
        
    def getpledge(self,name,passwd,table,pledge):
        
        if name == None:
            self.name = self.name
        else:
            self.name = name 
        if  passwd == None:
            self.passwd = md5.new(self.passwd).hexdigest()
        else:
            self.passwd = md5.new(passwd).hexdigest()
        if pledge == None:
            data_e = "Please insert name of priveledge field ! \n"
            return ['error',str(data_e)]
        else:
            self.pledge = pledge
        try:
            if self.pledge != None:
                print "len(pledge) = ", len(self.pledge), str(self.pledge)
            sql4 = "SELECT " + str(",").join(self.pledge) + " FROM " + table + " WHERE name = '" + self.name + "' AND password = '" + self.passwd + "' LIMIT 1"
                
            self.curs.execute(sql4)
            self.conn.commit()    
            data = self.curs.fetchall()
            return data
        except:
            data_e = traceback.format_exc()
            return ['error',str(data_e)]
            
    